//
//  ForgetViewController.m
//  DBAChain
//
//  Created by Mxionlly on 2018/9/6.
//  Copyright © 2018年 Evan. All rights reserved.
//

#import "ForgetViewController.h"
#import "taskUserManagerService.h"

@interface ForgetViewController ()
@property (weak, nonatomic) IBOutlet UIButton *codeButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *passField;
@property (weak, nonatomic) IBOutlet UITextField *passField1;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (nonatomic, strong) NSTimer           *timer;
@property (nonatomic, assign) NSInteger         number;
///短信收到的验证码
@property (nonatomic, strong) NSString*         verifyCode;

///1：新用户   0： 老用户
@property (nonatomic, assign) NSInteger         isNewUser;

@property (weak, nonatomic) IBOutlet UIButton *btnNewUser;

@property (weak, nonatomic) IBOutlet UIButton *btnOldUser;

@property (weak, nonatomic) IBOutlet UITextField *txtOldUserNo;

@property (weak, nonatomic) IBOutlet UILabel *labelOldLine;

@end

@implementation ForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"忘记密码";
    self.isNewUser = 1;
    [self initUI];
    [self setBlackStatus:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    self.hidesBottomBarWhenPushed = NO;
    if (self.tabBarController.tabBar.hidden == YES) {
        self.tabBarController.tabBar.hidden = NO;
    }
    if (self.navigationController.navigationBar.hidden == YES) {
        self.navigationController.navigationBar.hidden = NO;
    }
}


-(void)initUI{
    self.number = 60;
    self.codeButton.layer.masksToBounds = YES;
    self.codeButton.layer.borderWidth = 1.0f;
    self.codeButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.codeButton.layer.cornerRadius = 20.0f;
}

- (IBAction)getCodeAction:(id)sender {
    if (![Check isMobileNumber:self.phoneField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }else{
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setField:self.phoneField.text value:@"phone"];
        [taskUserManagerService getVerifyCode:dict success:^(dataGetVerifyCode *response, BOOL success) {
            if (success) {
                self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updataAction) userInfo:nil repeats:YES];
                self.codeButton.userInteractionEnabled = NO;
                self.verifyCode = response.data;
            }
        } failture:^(NSError *error, NSString *strDescription) {
        
        }];
    }
}

- (void)updataAction {
    self.number--;
    [self.codeButton setTitle:[NSString stringWithFormat:@"%lds",(long)self.number] forState:UIControlStateNormal];
    if (self.number <= 0) {
        [self closeTimer];
    }
}

- (void)closeTimer {
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
        self.number = 60;
        self.codeButton.userInteractionEnabled = YES;
        [self.codeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    }
}

- (IBAction)forgetAction:(id)sender {
    if (![Check isMobileNumber:self.phoneField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    if (![Check checkLenght:self.codeField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    if (![Check checkLenght:self.passField.text] || ![Check checkLenght:self.passField1.text]) {
        [DKProgressHUD showErrorWithStatus:@"请设置密码"];
        return;
    }
    if (![self.passField.text isEqualToString:self.passField1.text]) {
        [DKProgressHUD showErrorWithStatus:@"两次密码设置不一致"];
        return;
    }
    if (self.codeField.text != self.verifyCode) {
        [DKProgressHUD showErrorWithStatus:@"您输入的验证码有误"];
        return;
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setField:self.phoneField.text value:@"phone"];
    [dict setField:self.passField.text value:@"password"];
    [dict setField:self.codeField.text value:@"verifyCode"];
    [dict setField:self.passField1.text value:@"newPassword"];
    ///1：新用户   0： 老用户
    [dict setField:@(self.isNewUser) value:@"type"];
    if (self.isNewUser == 0) {
        if (![Check checkLenght:self.txtOldUserNo.text]) {
            [DKProgressHUD showErrorWithStatus:@"请输入用户编号"];
            return;
        }
        [dict setField:self.txtOldUserNo.text value:@"userNo"];
    }
    [DKProgressHUD showLoading];
    [taskUserManagerService getResetPassword: dict success:^(BaseModel *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            [DKProgressHUD showSuccessWithStatus:@"设置新密码成功"];
            [self goBack:nil];
        }else{
            [DKProgressHUD showErrorWithStatus:response.statusDesc];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnNewUserClick:(id)sender {
    self.isNewUser = 1;
    
    self.txtOldUserNo.hidden = YES;
    self.labelOldLine.hidden = YES;
    
    [self.btnNewUser setImage:[UIImage imageNamed:@"leftCheck"] forState:UIControlStateNormal];
    [self.btnOldUser setImage:[UIImage imageNamed:@"leftCheck_no"] forState:UIControlStateNormal];
}

- (IBAction)btnOldUserClick:(id)sender {
    self.isNewUser = 0;
    
    self.txtOldUserNo.hidden = NO;
    self.labelOldLine.hidden = NO;
    
    [self.btnOldUser setImage:[UIImage imageNamed:@"leftCheck"] forState:UIControlStateNormal];
    [self.btnNewUser setImage:[UIImage imageNamed:@"leftCheck_no"] forState:UIControlStateNormal];
}


@end
